﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iLangl.Customer.Actions;
using iLangl.Model;
using iLangl;
using iLangl.Customer.DAL;
using iLangl.Customer.Managers;
using iLangl.Plugin;
using System.Web.Security;

namespace iLangl.Customer.DNN
{
	public class RegistrationManager : BaseDNNManager
	{
		#region Singelton Pattern
		private RegistrationManager() : base()
		{
			
		}

		private static RegistrationManager instance;
		private static object mutex = new object();
		/// <summary>
		/// The instance of the RegistrationManager.
		/// </summary>
		public static RegistrationManager Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = new RegistrationManager();
							
						}
					}
				}
				return instance;
			}
		}
		#endregion

		public String Authenticate(String siteId, String userName, String password)
		{
			String result = MembershipCreateStatus.Success.ToString();

			IConfigurationManager manager = ConfigurationFactory.Instance.CreateConfigurationManager();
			IPluginConfiguration configuration = null;
			if (IsUserRegistered(siteId))
			{
				configuration = manager.GetPluginConfiguration(siteId);
			}
			else
			{
				result = Facade.Instance.AuthenticationService.RegisterUser(userName, password);
								
				configuration = ConfigurationFactory.Instance.CreatePluginConfiguration();
			}

			if (result == MembershipCreateStatus.Success.ToString())
			{

				configuration.SiteId = siteId;
				configuration.SetAccountUserName(userName);
				configuration.SetAccountPassword(password);

				ConfigurationManager.SavePluginConfiguration(configuration);
			}

			return result;
		}

		public const String PluginId = "97659FE2-2F41-4421-8E94-E95E5584B42C";
		public const String PluginName = "iLangl DotNetNuke 6.x Plugin";
		public const String PluginVersion = "1";

		public bool IsUserRegistered(String siteId)
		{
			IConfigurationManager manager = ConfigurationFactory.Instance.CreateConfigurationManager();
			IPluginConfiguration configuration = manager.GetPluginConfiguration(siteId);

			return (
				(!String.IsNullOrEmpty(configuration.AccountUserName))
				&&
				(!String.IsNullOrEmpty(configuration.AccountPassword))
				);
		}

		public bool IsPluginRegistered(String siteId)
		{
			return !String.IsNullOrEmpty(GetPluginLicenseKey(siteId));
		}

		public PluginRegistration GetPluginRegistration(String siteId)
		{
			PluginRegistration pluginRegistration = new PluginRegistration();

			pluginRegistration.PluginId = PluginId;
			pluginRegistration.PluginName = PluginName;
			pluginRegistration.PluginVersion = PluginVersion;
			pluginRegistration.PluginLicenseKey = GetPluginLicenseKey(siteId);


			return pluginRegistration;
		}

		private string GetPluginLicenseKey(String siteId)
		{
			return ConfigurationManager.GetPluginConfiguration(siteId).PluginLicenseKey;
		}


		public String RegisterLicense(String siteId, PluginRegistration pluginRegistrationRequest)
		{

			IPluginConfiguration pg = ConfigurationManager.GetPluginConfiguration(siteId);


			pluginRegistrationRequest.SessionId = new PluginAuthenticationService().Authenticate(siteId);
			//Define UserId
			string userId = Facade.Instance.AuthenticationService.GetUserName(pluginRegistrationRequest.SessionId);

			//Register Company
			CompanyManager.Instance.CreateCompany((CustomerCompany)pluginRegistrationRequest.Company, pluginRegistrationRequest.PrimaryContactId, userId);

			if (!IsPluginRegistered(siteId))
			{

				pluginRegistrationRequest.PushURIAddress = PushServiceManager.Instance.GetUrl();

				RegisterPluginAction registerPluginAction = new RegisterPluginAction(pluginRegistrationRequest);

				registerPluginAction.Execute();


				pg.SetPluginLicenseKey(registerPluginAction.PluginLicenceKey);
				pg.SetPublishURI(pluginRegistrationRequest.PushURIAddress);

				ConfigurationManager.SavePluginConfiguration(pg);
			}
			else
			{
				UpdateCompanyInfoAction updatecompany = new UpdateCompanyInfoAction(pluginRegistrationRequest.Company);
				updatecompany.Execute();
			}


			//Read Settings 
			SettingsManager.Instance.DownloadiLanglSettings();

			return pg.PluginLicenseKey;
		}


		public void SavePluginRegistration(PluginRegistration plugRegistration)
		{
			//Register Company
			CompanyManager.Instance.CreateCompany((CustomerCompany)plugRegistration.Company);

		}

	}
}